package com.hystrix.controller;

import javax.servlet.http.HttpServletRequest;

import com.alibaba.fastjson.JSONObject;
import com.hystrix.entity.Person;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

@RestController
public class HelloController {

	@GetMapping("/hello")
	public String normalHello(HttpServletRequest request) {
		return "Hello World";
	}
	
	@GetMapping("/errorHello")
	public String errorHello(HttpServletRequest request) throws Exception {
		// 模拟需要处理10秒，导致连接出现超时的情况
		Thread.sleep(10000);
		return "Error Hello World";
	}

	@PostMapping("/personList")
	public Map personList(@RequestBody String [] persons) {
		Map<String, Person> result = new HashMap<>();
		Arrays.stream(persons).forEach(psersonName->{
			Random r = new Random();
			Person p = new Person();
			p.setId(r .nextLong());
			p.setName(psersonName);
			result.put(psersonName, p);
		});
		return  result;
	}



}
